#! /bin/bash

# setup script that is only run once at boot time
# set up an FAI install server

NORMAL='\E(B\E[m'
RED='\E[31m'
GREEN='\E[32m'

set -o pipefail

# setup network
nic=$(awk '/iface/ {print $2}' /etc/network/interfaces |egrep -v ^lo)
ifup $nic
# regenerate ssh_host keys
ls /etc/ssh/ssh_host_* > /dev/null
if [ $? -ne 0 ]; then
    dpkg-reconfigure -fnoninteractive openssh-server
fi
sleep 8
[ -x /etc/init.d/nscd ] && invoke-rc.d nscd restart

echo "================================="
echo "Setting up the FAI install server"
echo "This will take a few minutes"
echo "=================================" 

. /etc/fai/fai.conf
. /etc/fai/nfsroot.conf

# copy the simple examples and pimp my config space
if [ ! -d "$FAI_CONFIGDIR/class" ]; then
    mkdir -p $FAI_CONFIGDIR
    cp -a /usr/share/doc/fai-doc/examples/simple/* $FAI_CONFIGDIR
    ainsl /srv/fai/config/class/FAIBASE.var "^LOGUSER=fai"
    myip=$(ip addr show up| grep -w inet | cut -d t -f 2 | cut -d ' ' -f 2 | cut -d / -f 1 | grep -v 127.0.0.1)
    echo "APTPROXY=http://$myip:3142" >> /srv/fai/config/class/DEBIAN.var
fi
# set the LOGUSER, wo we get all the logs from our install clients
ainsl /etc/fai/fai.conf "^LOGUSER=fai"

# make index, then import the packages from the CD mirror
apt-get update >/dev/null
curl -fs 'http://127.0.0.1:3142/acng-report.html?doImport=Start+Import&calcSize=cs&asNeeded=an#bottom' >/dev/null

# setup the FAI server, including creating the nfsroot, use my own proxy
export APTPROXY="http://127.0.0.1:3142"
if [ -f /var/tmp/base.tar.xz ]; then
    fai-setup -fvB /var/tmp/base.tar.xz > /var/log/fai/fai-setup.log 2>&1
else
    fai-setup -fv > /var/log/fai/fai-setup.log 2>&1
fi
if [ $? -eq 0 ]; then
    rm /var/tmp/base.tar.xz
    echo ""
    echo "================================================" 
    echo -e "Setting up the FAI server was ${GREEN}successful${NORMAL}"
    echo "================================================" 
    echo ""
    sleep 10
else
    echo ""
    echo "=================================================="
    echo -e "${RED}ERROR${NORMAL}: Setting up the FAI install server ${RED}FAILED${NORMAL}!"
    echo "Read /var/log/fai/fai-setup.log for more debugging"
    echo "=================================================="
    echo ""
    sleep 10
    exit 99
fi

cat <<EOF >> /srv/fai/nfsroot/etc/fai/fai.conf
# use short hostname instead of FQDN
export HOSTNAME=\${HOSTNAME%%.*}
echo \$HOSTNAME > /proc/sys/kernel/hostname
EOF

# create default pxelinux boot configuration
fai-chboot -o default

# create a template for booting the installation
fai-chboot -Iv -f verbose,sshd,createvt,menu -u nfs://faiserver/srv/fai/config stretch.tmpl

# Since we do not know the MAC address, our DHCP cannot provide the hostname.
# Therefore we do explicitly set the hostname
fai-chboot -Iv -f verbose,sshd,createvt,menu -u nfs://faiserver/srv/fai/config -k hostname=xfcehost xfcehost
fai-chboot -Iv -f verbose,sshd,createvt,menu -u nfs://faiserver/srv/fai/config -k hostname=demohost demohost
for c in {01..10}; do
    fai-chboot -Iv -f verbose,sshd,createvt,menu -u nfs://faiserver/srv/fai/config -k hostname=client$c client$c
done

fai-monitor > /var/log/fai/fai-monitor.log &

# move me away
mv $0 /var/tmp

# create new rc.local for next reboot
echo '#! /bin/bash' > /etc/rc.local
echo 'fai-monitor > /var/log/fai/fai-monitor.log &' >> /etc/rc.local
chmod a+rx /etc/rc.local

exit 0
